package handler

import (
	"gitee.com/hp-huiw/family-expense-api/internal/constant"
	"gitee.com/hp-huiw/family-expense-api/internal/service"
	"gitee.com/hp-huiw/my-go-core/config"
	"github.com/gin-gonic/gin"
)

type DataInitializationHandler interface {
	RetrieveAll(ctx *gin.Context)
}

func NewDataInitializationHandler() DataInitializationHandler {
	return &dataInitializationHandler{}
}

// Matches /queryAllWithBasicInfo of http get action.
func (*dataInitializationHandler) RetrieveAll(ctx *gin.Context) {
	c := ctx.Request.Context()
	db := GetDatabase(ctx)
	claims := GetClaims(ctx)
	accountGroupID := claims.AccountGroupID
	userID := claims.UserID
	accountGroupInfo, err := service.NewAccountGroupService(db).QueryInfo(c, accountGroupID, userID)

	if err != nil {
		ctx.Error(err)
		return
	}

	bankCards, err := service.NewBankCardService(db).QueryAllWithBasicInfo(c, accountGroupID)

	if err != nil {
		ctx.Error(err)
		return
	}

	detailTypes, err := service.NewIncomeExpenseDetailTypeService(db).Query(c, accountGroupID, "")

	if err != nil {
		ctx.Error(err)
		return
	}

	incomeExpenseType := map[string]any{
		constant.IncomeExpenseTypeExpense: "支出",
		constant.IncomeExpenseTypeIncome:  "收入",
	}

	paymentType := map[string]any{
		constant.PaymentTypeCash:       "现金",
		constant.PaymentTypeCreditCard: "信用卡",
	}

	SendDataResponse(ctx, map[string]any{
		"accountGroupInfo":         accountGroupInfo,
		"bankCards":                bankCards,
		"detailTypes":              detailTypes,
		"incomeExpenseType":        incomeExpenseType,
		"paymentType":              paymentType,
		"recentSearchListMaxCount": config.ConfigLoader.GetAppConfig().AppConfig.RecentSearchListMaxCount,
		"baiduMap": map[string]any{
			"androidKey": config.ConfigLoader.GetAppConfig().BaiduMap.AndroidKey,
		},
		"amap": map[string]any{
			"androidKey": config.ConfigLoader.GetAppConfig().AMap.AndroidKey,
			"webApiKey":  config.ConfigLoader.GetAppConfig().AMap.WebApiKey,
		},
	})
}

// Container of DataInitializationHandler functions.
type dataInitializationHandler struct{}
